package gemini.castor.ui.client.page.context.shoppingcartpreview;

import gemini.castor.ui.client.mvp.gin.CastorGinjector;
import gemini.castor.ui.client.page.navigation.NavigationEvent;
import gemini.castor.ui.client.utils.PlaceConstants.NavigationItem;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ShoppingCartPreviewWidget extends Composite implements HasHandlers{

	@UiTemplate("ShoppingCartPreviewWidget.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, ShoppingCartPreviewWidget> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	public ShoppingCartPreviewWidget() {
		initWidget(binder.createAndBindUi(this));
	}
	
	public void setCartId(String cartId){
		htmCartId.setHTML(cartId);
	}

	@UiField
	VerticalPanel pnlRecentlyAdded;
	
	@UiField
	FlexTable ftbRecentlyAdded;
	
	@UiField
	HTML htmProducts;
	
	@UiField
	HTML htmCartId;
	
	@UiField
	HTML htmItems;
	
	@UiField
	HTML htmPoint;
	
	@UiField
	HTML htmOrderMonth;
	
	@UiField
	HTML htmMonthlyPoint;
	
	@UiField
	HTML htmDiscountScale;
	
	@UiField
	Button btnCheckOut;
	
	@UiHandler("btnCheckOut")
	void onCheckOut(ClickEvent e) {
		CastorGinjector.INSTANCE.getEventBus().fireEvent(new NavigationEvent(NavigationItem.SHOPPING_CART));
	}
	
	public void loadData(ShoppingCartPreviewObject object){
		
		int products = 0;
		int items = 0;
		
		
		if(object.getItems() != null && !object.getItems().isEmpty()){
			products = object.getItems().size();
			
			ColumnFormatter colFmt = ftbRecentlyAdded.getColumnFormatter();
			
			colFmt.setWidth(0, "10%");
			colFmt.setWidth(1, "90%");
			
			ftbRecentlyAdded.removeAllRows();
			
			int i = 0;
			for (ShoppingCartPreviewItem it : object.getItems()) {
				items += it.getQuantity();
					
				ftbRecentlyAdded.setHTML(i, 0, String.valueOf(it.getQuantity()));
				ftbRecentlyAdded.setHTML(i, 1, it.getName());
				
				i++;
			}
			pnlRecentlyAdded.setVisible(true);
			btnCheckOut.setVisible(true);
		} else {
			pnlRecentlyAdded.setVisible(false);
			btnCheckOut.setVisible(false);
		}
		
		htmProducts.setHTML(String.valueOf(products));
		htmItems.setHTML(String.valueOf(items));
		htmPoint.setHTML(String.valueOf(object.getPoint()));

		htmOrderMonth.setHTML(object.getOrderMonth());
		
		htmMonthlyPoint.setHTML(object.getMonthlyPointString());
		htmDiscountScale.setHTML(String.valueOf(object.getDiscountScale()));

	}
	
	private boolean closed = true;

	public boolean isClosed() {
		return closed;
	}

	public void setClosed(boolean closed) {
		this.closed = closed;
	}
}
